package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.entity.Banner;
import com.itheima.result.Result;
import com.itheima.service.BannerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/banner")
public class BannerController {
    private final BannerService service;

    @GetMapping("/list")
    public Result findByPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize,
            String title) {
        //调用service完成分页条件查询
        PageInfo pageInfo = service.findByPage(pageNum, pageSize, title);

        return Result.success(pageInfo);
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        Banner banner = service.findById(id);
        return Result.success(banner);
    }

    @DeleteMapping("/{ids}")
    public Result removeById(@PathVariable String ids){
        List<String> list = Arrays.asList(ids.split(","));
        return service.removeById(list)
                ? Result.success()
                : Result.error("服务器错误，删除失败");
    }
}
